﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maticsoft.Web
{
    public partial class ListMN : System.Web.UI.Page
    {

        BLL.studentInfo s = new BLL.studentInfo();
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["type"] != null)
                {
                    string type = Server.UrlDecode(Request.QueryString["type"].ToString());
                    //[sCount] ,[mCount] ,[jCount] ,[sScore] ,[mScore] ,[jScore] 
                    //type = "机关单位";
                    //lblssum.Text = "20";
                    //lblmsum.Text = "10";
                    //lbljsum.Text = "20";
                    //lblsfen.Text = "2";
                    //lblmfen.Text = "2";
                    //lbljfen.Text = "2";
                    Bind(type);
                    //BindAnswer(stuId);   
                }
            }

        }
        private void Bind(string type)
        {
            ds = s.GetPaperDetailbytype("模拟");

            if (ds.Tables[0].Rows.Count == 1)
            {
                hfscount.Value = ds.Tables[0].Rows[0][0].ToString();
                hfmcount.Value = ds.Tables[0].Rows[0][1].ToString();
                hfjcount.Value = ds.Tables[0].Rows[0][2].ToString();
                hfsscore.Value = ds.Tables[0].Rows[0][3].ToString();
                hfmscore.Value = ds.Tables[0].Rows[0][4].ToString();
                hfjscore.Value = ds.Tables[0].Rows[0][5].ToString();
                hfgtime.Value = ds.Tables[0].Rows[0][6].ToString();
                hfjige.Value = ds.Tables[0].Rows[0][7].ToString();
                Maticsoft.BLL.singleInfo sm = new BLL.singleInfo();
                ds = sm.GetList(" stutype='" + type + "' order by newid()", hfscount.Value);
                this.DataList1.DataSource = ds;
                this.DataList1.DataBind();
                Maticsoft.BLL.mostInfo mm = new BLL.mostInfo();
                ds = mm.GetList(" stutype='" + type + "' order by newid()", hfmcount.Value);
                this.DataList2.DataSource = ds;
                this.DataList2.DataBind();
                Maticsoft.BLL.judgeInfo jm = new BLL.judgeInfo();
                ds = jm.GetList(" stutype='" + type + "' order by newid()", hfjcount.Value);
                this.DataList3.DataSource = ds;
                this.DataList3.DataBind();
            }
        }

        //提交答案
        protected void btn_submit_Click(object sender, EventArgs e)
        {


            //保存时间


        }
        protected int[] timeload(string a)
        {
            int[] t = new int[3];
            t[0] = int.Parse(a.Split(':')[0]);
            t[1] = int.Parse(a.Split(':')[1]);
            t[2] = int.Parse(a.Split(':')[2]);
            return t;
        }
        private void SubmitQuestion()
        {
            string mark = "", zuo = "", nozuo = "";
            int s = 0, m = 0, j = 0;
            //km.DeleteByWhere(" stuId=" + stuId + " and type = 1 ");//删除此学生的所有已提交单项选择答案
            foreach (DataListItem item in this.DataList1.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                // Label lbl_singleId = (Label)item.FindControl("lbl_singleId");
                RadioButton rdoA = (RadioButton)item.FindControl("rdoA");
                RadioButton rdoB = (RadioButton)item.FindControl("rdoB");
                RadioButton rdoC = (RadioButton)item.FindControl("rdoC");
                RadioButton rdoD = (RadioButton)item.FindControl("rdoD");
                //string key = ((HiddenField)item.FindControl("hfkey")).Value;
                //string m = ((HiddenField)item.FindControl("hfmark")).Value;
                Panel pz = item.FindControl("pz") as Panel;
                Panel pd = item.FindControl("pd") as Panel;
                Label l = (Label)item.FindControl("lblr");
                string key = l.Text;
                //string title = lbl_title.Text;
                //string singleId = lbl_singleId.Text;//题目ID 
                Label mykey = item.FindControl("lblkey") as Label;
                string result = "";
                if (rdoA.Checked)
                {
                    result = "A";
                }
                if (rdoB.Checked)
                {
                    result = "B";
                }
                if (rdoC.Checked)
                {
                    result = "C";
                }
                if (rdoD.Checked)
                {
                    result = "D";
                }
                mykey.Text = result;
                if (result == key)
                    s++;
                pz.Visible = true;
                pd.Visible = false;


            }
            foreach (DataListItem item in this.DataList2.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                //Label lbl_mostId = (Label)item.FindControl("lbl_mostId");

                CheckBox cboA = (CheckBox)item.FindControl("cboA");
                CheckBox cboB = (CheckBox)item.FindControl("cboB");
                CheckBox cboC = (CheckBox)item.FindControl("cboC");
                CheckBox cboD = (CheckBox)item.FindControl("cboD");
                Label l = (Label)item.FindControl("lblr");
                string key = l.Text;
                //string m = ((HiddenField)item.FindControl("hfmark")).Value;
                //Label l = (Label)item.FindControl("lblan");
                //string title = lbl_title.Text;
                ///string mostId = lbl_mostId.Text;//题目ID 
                Panel pz = item.FindControl("pz") as Panel;
                Panel pd = item.FindControl("pd") as Panel;
                //Label l = (Label)item.FindControl("lblan");
                //string title = lbl_title.Text;
                //string singleId = lbl_singleId.Text;//题目ID 
                Label mykey = item.FindControl("lblkey") as Label;

                string result = "";

                if (cboA.Checked)
                {
                    result += "A";
                }
                if (cboB.Checked)
                {
                    result += "B";
                }
                if (cboC.Checked)
                {
                    result += "C";
                }
                if (cboD.Checked)
                {
                    result += "D";
                }
                mykey.Text = result;
                if (result == key)
                    m++;
                pz.Visible = true;
                pd.Visible = false;


            }
            foreach (DataListItem item in this.DataList3.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                //Label lbl_judgeId = (Label)item.FindControl("lbl_judgeId");
                RadioButton rdoYes = (RadioButton)item.FindControl("rdoYes");
                RadioButton rdoNo = (RadioButton)item.FindControl("rdoNo");
                Label l = (Label)item.FindControl("lblr");
                string key = l.Text;
                // string m = ((HiddenField)item.FindControl("hfmark")).Value;
                //Label l = (Label)item.FindControl("lblan");
                Panel pz = item.FindControl("pz") as Panel;
                Panel pd = item.FindControl("pd") as Panel;
                //Label l = (Label)item.FindControl("lblan");
                //string title = lbl_title.Text;
                //string singleId = lbl_singleId.Text;//题目ID 
                Label mykey = item.FindControl("lblkey") as Label;
                //
                string result = "";

                if (rdoYes.Checked)
                {
                    result = "正确";
                }
                if (rdoNo.Checked)
                {
                    result = "错误";
                }
                if (result == key)
                    j++;
                mykey.Text = result;
                pz.Visible = true;
                pd.Visible = false;
            }
            float ss = float.Parse(hfsscore.Value);
            float ms = float.Parse(hfmscore.Value);
            float js = float.Parse(hfjscore.Value);
            lbWarnResult.Text = "您的考试成绩："+(s*ss+m*ms+j*js)+"分，通过分："+hfjige.Value+"分";
            pfen.Visible = true;
            btnSub.Visible = false;
            btnCheck.Visible = false;
            btnSub0.Visible = false;
            btnCheck0.Visible = false;
        }

        private string checkQuestion()
        {
            string mark = "", zuo = "", nozuo = "";
            int ss = 0, mm = 0, j = 0;
            ss = int.Parse(hfscount.Value)+1;
            mm = int.Parse(hfmcount.Value)+1;
            foreach (DataListItem item in this.DataList1.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                //Label lbl_singleId = (Label)item.FindControl("lbl_singleId");
                RadioButton rdoA = (RadioButton)item.FindControl("rdoA");
                RadioButton rdoB = (RadioButton)item.FindControl("rdoB");
                RadioButton rdoC = (RadioButton)item.FindControl("rdoC");
                RadioButton rdoD = (RadioButton)item.FindControl("rdoD");
                //string key = ((HiddenField)item.FindControl("hfkey")).Value;
                string m = ((HiddenField)item.FindControl("hfmark")).Value;
                //Label l = (Label)item.FindControl("lblan");
                //string title = lbl_title.Text;
                //string singleId = lbl_singleId.Text;//题目ID 

                string result = "";

                if (rdoA.Checked)
                {
                    result = "A";
                }
                if (rdoB.Checked)
                {
                    result = "B";
                }
                if (rdoC.Checked)
                {
                    result = "C";
                }
                if (rdoD.Checked)
                {
                    result = "D";
                }
                if (result == "")
                {
                    nozuo += item.ItemIndex + 1 + ",";

                }
                else
                {
                    zuo += item.ItemIndex + 1 + ",";
                }
                if (m.StartsWith("M"))
                {
                    mark += item.ItemIndex + 1 + ",";
                }

            }
            foreach (DataListItem item in this.DataList2.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                //Label lbl_mostId = (Label)item.FindControl("lbl_mostId");

                CheckBox cboA = (CheckBox)item.FindControl("cboA");
                CheckBox cboB = (CheckBox)item.FindControl("cboB");
                CheckBox cboC = (CheckBox)item.FindControl("cboC");
                CheckBox cboD = (CheckBox)item.FindControl("cboD");
                //string key = ((HiddenField)item.FindControl("hfkey")).Value;
                string m = ((HiddenField)item.FindControl("hfmark")).Value;
                //Label l = (Label)item.FindControl("lblan");
                //string title = lbl_title.Text;
                //string mostId = lbl_mostId.Text;//题目ID 

                string result = "";

                if (cboA.Checked)
                {
                    result += "A";
                }
                if (cboB.Checked)
                {
                    result += "B";
                }
                if (cboC.Checked)
                {
                    result += "C";
                }
                if (cboD.Checked)
                {
                    result += "D";
                }
                if (result == "")
                {
                    nozuo += item.ItemIndex + ss + ",";

                }
                else
                {
                    zuo += item.ItemIndex + ss + ",";
                }
                if (m.StartsWith("M"))
                {
                    mark += item.ItemIndex + ss + ",";
                }

            }
            foreach (DataListItem item in this.DataList3.Items)
            {
                //Label lbl_title = (Label)item.FindControl("lbl_title");
                //Label lbl_judgeId = (Label)item.FindControl("lbl_judgeId");
                RadioButton rdoYes = (RadioButton)item.FindControl("rdoYes");
                RadioButton rdoNo = (RadioButton)item.FindControl("rdoNo");
                //string key = ((HiddenField)item.FindControl("hfkey")).Value;
                string m = ((HiddenField)item.FindControl("hfmark")).Value;
                //Label l = (Label)item.FindControl("lblan");
                //string title = lbl_title.Text;
                //string judgeId = lbl_judgeId.Text;//题目ID 

                string result = "";

                if (rdoYes.Checked)
                {
                    result = "正确";
                }
                if (rdoNo.Checked)
                {
                    result = "错误";
                }
                if (result == "")
                {
                    nozuo += item.ItemIndex + ss+mm-1 + ",";

                }
                else
                {
                    zuo += item.ItemIndex + ss + mm - 1 + ",";
                }
                if (m.StartsWith("M"))
                {
                    mark += item.ItemIndex + ss + mm - 1 + ",";
                }


            }
            if (mark != "") mark = mark.Substring(0, mark.Length - 1);
            if (zuo != "") zuo = zuo.Substring(0, zuo.Length - 1);
            if (nozuo != "") nozuo = nozuo.Substring(0, nozuo.Length - 1);
            string s = mark + "@" + zuo + "@" + nozuo;
            //sethtml(s);
            return s;
        }
        protected void sethtml(string at)
        {
            string[] marks, zuos, nozuos;
            string a = "";
            string[] ss = at.Split('@');
            marks = ss[0].Split(',');
            zuos = ss[1].Split(',');
            nozuos = ss[2].Split(',');
            if (marks[0] != "")
                lblmark.Text = marks.Length.ToString();
            else
                lblmark.Text = "0";
            if (zuos[0] != "")
                lblask.Text = zuos.Length.ToString();
            else
                lblask.Text = "0";
            if (nozuos[0] != "")
                lblnoask.Text = nozuos.Length.ToString();
            else
                lblnoask.Text = "0";
            lblna.Text = lblnoask.Text;
            if (marks.Length != 0 && marks[0] != "")
            {
                a = "<tbody><tr>";
                //<input name="btnTemp2" class="btnNumber" id="btnTemp2" onclick="Javascript:return numberClicked('2');__doPostBack('btnTemp2','')" type="button" value="第3题">
                for (int i = 0; i < marks.Length; i++)
                {
                    a += "<td><input class=\"btnNumber\" onclick=\"Javascript: return numberClicked('" + marks[i] + "');\" type=\"button\" value=\"第" + marks[i] + "题\"></td>";
                    if (i != 0 && (i+1) % 10 == 0)
                    {
                        a += "</tr><tr>";
                    }

                }
                a += "</tr></tbody>";
            }
            lmark.Text = a;
            a = "";
            if (zuos.Length != 0 && zuos[0] != "")
            {
                a = "<tbody><tr>";

                for (int i = 0; i < zuos.Length; i++)
                {
                    a += "<td><input class=\"btnNumber\" onclick=\"Javascript: return numberClicked('" + zuos[i] + "');\" type=\"button\" value=\"第" + zuos[i] + "题\"></td>";
                    if (i != 0 && (i + 1) % 10 == 0)
                    {
                        a += "</tr><tr>";
                    }

                }
                a += "</tr></tbody>";
            }
            lask.Text = a;
            a = "";
            if (nozuos.Length != 0 && nozuos[0] != "")
            {
                a = "<tbody><tr>";

                for (int i = 0; i < nozuos.Length; i++)
                {
                    a += "<td><input class=\"btnNumber\" onclick=\"Javascript: return numberClicked('" + nozuos[i] + "');\" type=\"button\" value=\"第" + nozuos[i] + "题\"></td>";
                    if (i != 0 && (i + 1) % 10 == 0)
                    {
                        a += "</tr><tr>";
                    }

                }
                a += "</tr></tbody>";
            }
            lnoask.Text = a;
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            //SubmitQuestion();
            checkQuestion();


        }

        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField h = e.Item.FindControl("hfmark") as HiddenField;
                Button b = e.Item.FindControl("btnMark") as Button;
                Label l3 = e.Item.FindControl("Label4") as Label;
                Label l4 = e.Item.FindControl("Label3") as Label;
                RadioButton r4 = e.Item.FindControl("rdoD") as RadioButton;
                RadioButton r3 = e.Item.FindControl("rdoC") as RadioButton;
                if (l4 != null)
                {
                    if (l4.Text.Trim() == "D：") { l4.Visible = false; if (r4 != null)r4.Visible = false; }
                }
                if (l3 != null)
                {
                    if (l3.Text.Trim() == "C：") { l3.Visible = false; if (r3 != null)r3.Visible = false; }
                }
                if (b != null && h != null)
                {
                    b.Attributes.Add("onclick", "javascript:return changeColor(this,'" + h.ClientID + "');");
                    //b.Attributes.Add("onclick", "javascript:alert('asdf'); return false;");

                }
            }
        }

        protected void btnSub_Click(object sender, EventArgs e)
        {
            string rs = checkQuestion();
            if (rs.Split('@')[1].Split(',').Length == 0)
            {
                SubmitQuestion();

            }
            else
            {
                sethtml(rs);
                btnOK1.Visible = true;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "b", "clickcheck();", true);
            }


        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            string rs = checkQuestion();
            sethtml(rs);
            btnOK1.Visible = false;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "b", "clickcheck();", true);

        }

        protected void btnOK1_Click(object sender, EventArgs e)
        {
            SubmitQuestion();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "b", "window.top.location.href = 'index.aspx'", true);
        }

        protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField h = e.Item.FindControl("hfmark") as HiddenField;
                Button b = e.Item.FindControl("btnMark") as Button;
                Label l3 = e.Item.FindControl("Label5") as Label;
                Label l4 = e.Item.FindControl("Label6") as Label;
                RadioButton r4 = e.Item.FindControl("rdoD") as RadioButton;
                RadioButton r3 = e.Item.FindControl("rdoC") as RadioButton;
                if (l4 != null)
                {
                    if (l4.Text.Trim() == "D：") { l4.Visible = false; if (r4 != null)r4.Visible = false; }
                }
                if (l3 != null)
                {
                    if (l3.Text.Trim() == "C：") { l3.Visible = false; if (r3 != null)r3.Visible = false; }
                }
                if (b != null && h != null)
                {
                    b.Attributes.Add("onclick", "javascript:return changeColor(this,'" + h.ClientID + "');");
                    //b.Attributes.Add("onclick", "javascript:alert('asdf'); return false;");

                }
            }
        }
    }
}